#!/opt/lampp/bin/perl -w
use strict;
use warnings;
use DBI;

require('function.pl');
my $db_host = 'localhost';
my $db = 'crm';
my $db_user = 'zjxia';
my $db_password = 'bblovebb';
my $db_conn = DBI->connect("DBI:mysql:database=$db;host=$db_host", $db_user, $db_password);
# $db_conn->do('set character_set_client =gb2312; set character_set_connection =gb2312; set character_set_database =gb2312; set character_set_results =gb2312; set character_set_server =gb2312; set character_set_system =gb2312; SET collation_server = gb2312_chinese_ci; SET collation_database = gb2312_chinese_ci; SET collation_connection =gb2312_chinese_ci');
$db_conn->do('set names gb2312');
my %keywords = (
#二手
'电脑' => 'diannao'
, '数码' => 'shuma'
, '手机' => 'shouji'
, '家电' => 'jiadian'
, '二手家具' => 'ershoujiaju'
, '婴幼' => 'yingyou'
, '服饰' => 'fushi'
, '图书' => 'tushu'
, '网游' => 'wangyou'
, '文体' => 'wenti'
, '艺术' => 'yishu'
, '办公' => 'bangong'
, '二手设备' => 'ershoushebei'
, '通讯业务' => 'tongxunyw'
#招聘
, '销售' => 'yewu'
, '人事' => 'renli'
, '客服' => 'kefu'
, '旅游酒店' => 'zplvyoujiudian'
, '美容健身' => 'meirongjianshen'
, '市场营销' => 'shichang'
, '房产建筑' => 'zpfangchanjianzhu'
, '生产开发' => 'zpshengchankaifa'
, '计算机' => 'tech'
, '汽车' => 'zpqiche'
, '超市商业' => 'chaoshishangye'
, '财务审计' => 'zpcaiwushenji'
, '家政保洁' => 'jiazhengbaojiexin'
, '司机' => 'siji'
, '编辑' => 'zpxiezuochuban'
, '教育培训' => 'zhuanye'
, 'fanyi' => 'fanyizhaopin'
, '文体影视' => 'zpwentiyingshi'
, '广告' => 'zpguanggao'
, '金融投资' => 'jinrongtouzi'
, '保险' => 'zpjinrongbaoxian'
, '商务贸易' => 'zp商务贸易'
, "仓储/物流" => "zpwuliucangchu"
, "法律" => "zpfalvzixun"
, "医疗/制药/生物" => "zpyiyuanyiliao"
, "电子/电气/仪器仪表" => "zpjixieyiqi"
, "质控/安防" => "zhikonganfang"
, "高级管理" => "zpguanli"
, "服装/纺织/食品" => "xiaofeipin"
, "环境科学/环保" => "huanbao"
, "农/林/牧/渔业" => "nonglinmuyu"
, "其他职能" => "zhaopin"
#兼职
, "计算机/网络" => "jisuanjiwl"
, "图形/影像/设计" => "computer"
, "翻译/编辑" => "yuyanwenzi"
, "销售/促销" => "xiaoshoucuxiao"
, "客服/市场调查" => "kefushichang"
, "财务/法律" => "jianzhicaiwu"
, "教育培训" => "jiaoyupeixun"
, "餐饮/服务/旅游" => "canyinfuwu"
, "礼仪/演艺" => "liyiyanyi"
, "学生兼职/实习" => "xueshengjianzhi"
, "其他兼职" => "partime"
#家政服务
, "搬家" => "banjia"
, "保姆/月嫂" => "baomu"
, "保洁清洗" => "baojie"
, "家具维修" => "jiajuweixiu"
, "家电维修" => "dianqi"
, "电脑维修" => "weixiu"
, "房屋维修/防水" => "fangweixiu"
, "管道疏通" => "shutong"
, "鲜花" => "lipinxianhua"
, "二手回收" => "huishou"
, "生活配送" => "songshui"
, "洗衣店" => "ganxi"
, "殡葬" => "binzang"
#装修建材
, "基础建材" => "jichujc"
, "橱柜" => "chugui"
, "卫浴洁具" => "taoci"
, "厨卫电器" => "chuweidq"
, "瓷砖" => "cizdz"
, "地板" => "diban"
, "房门" => "fangmen"
, "移门壁柜" => "yimenbg"
, "涂料壁纸" => "tulyq"
, "开关插座" => "kaiguancz"
, "灯具照明" => "dengju"
, "塑钢门窗" => "menchuang"
, "五金工具" => "wujinlei"
, "电子电工" => "dianzidg"
, "床" => "chuangsp"
, "床垫" => "chuangdian"
, "榻榻米" => "tatam"
, "空间家具" => "kongjianjj"
, "沙发" => "shafasp"
, "桌类" => "chajilei"
, "椅凳" => "yizilei"
, "柜类" => "guilei"
, "架类" => "jialei"
, "几类" => "jilei"
, "案/台类" => "antail"
, "根雕类" => "gendiaol"
, "镜子类" => "jingzil"
, "屏风/花窗" => "pingfenghc"
, "床上用品" => "chuangpin"
, "布艺软饰" => "jiajubuyi"
, "帘类" => "lianlei"
, "毯类" => "tanlei"
, "垫类" => "dianlei"
, "环保/除味/保养" => "huanbaocwby"
, "装饰品" => "zhuangship"
, "挂件" => "guajian"
, "摆件" => "baijian"
, "画类" => "huakuang"
, "贴饰壁饰" => "tieshi"
, "雕刻工艺" => "diaokebj"
, "家装设计" => "zhuangxiu"
, "家装施工" => "shineizx"
, "施工监理" => "shigongjl"
, "工程装修" => "zhuangxiudui"
, "办公装修" => "zshi"
, "商业装修" => "changsuo"
#商务服务
, "工商注册" => "zhuce"
, "商标专利" => "shangbiaozhli"
, "担保/保险/投资" => "danbaobaoxiantouzi"
, "法律咨询" => "lvshi"
, "财务会计/评估" => "caishui"
, "咨询" => "zixunzhongjie"
, "货运专线" => "wuliu"
, "货运物流" => "huoyun"
, "快递" => "kuaidi"
, "喷绘招牌" => "penhui"
, "印刷包装" => "yinshua"
, "制卡" => "allzhika"
, "网站建设/推广" => "wangzhan"
, "租赁" => "zulin"
, "办公设备维修" => "bgsbwx"
, "网络维护及布线" => "xitong"
, "翻译/速记" => "fanyi"
);
my $state_file_name = "data/58_state";
my ($start_city_en, $start_keyword_en);
if (-e $state_file_name) {
	open IN, "$state_file_name";
	$_ = <IN>;
	($start_city_en, $start_keyword_en) = split("\t");
	close IN;
}
my %cities = load_map('city_map_58');
while (my ($city_cn, $city_en) = each %cities) {
	if (defined($start_city_en) && $start_city_en ne $city_en) {
#		print "$start_city_en\t$city_en\n";
		next;
	}
	else {
		undef $start_city_en;
	}
	while (my ($keyword_cn, $keyword_en) = each %keywords) {
		if (defined($start_keyword_en) && $start_keyword_en ne $keyword_en) {
#			print "$start_keyword_en\t$keyword_en\n";
			next;
		}
		else {
			open OUT, ">$state_file_name";
			print OUT "$city_en\t$keyword_en";
			close OUT;
			undef $start_keyword_en;
		}
		my $target_url = "http://$city_en.58.com/$keyword_en/";
		my $list_url = "http://localhost/meta_search/58lib/58_list.php?textcode=$target_url";
		print "$list_url\n";
		my $list_content = `curl -s --speed-time 5 --speed-limit 50000 --connect-timeout 60 -m 300 $list_url 2>/dev/null`;
		my @info_urls = split("\n", $list_content);
		foreach my $info_url (@info_urls) {
			`/opt/lampp/bin/perl 58_parser.pl '$info_url' '$city_cn' '$keyword_cn' >>log/58.log 2>>log/58.err &`;
			sleep(1);
			next;
		}
	}
}
`rm $state_file_name`;
